//
//  MVVMViewController.m
//  项目二
//
//  Created by rimi on 16/5/26.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "MVVMViewController.h"
#import "MVVMViewModel.h"

static  NSString * const Mcell = @"Mcell";
static  NSString * const kVCPath = @"model.images";

@interface MVVMViewController ()<UITableViewDataSource>
/**  VM小型控制器 */
@property (nonatomic, strong) MVVMViewModel *viewModel;
/**  UITableView */

@property (nonatomic, strong) UITableView *tableView;
@end

@implementation MVVMViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self registerKVC];
     [self initData];
    [self initUI];
   
}

#pragma mark - 实例方法
- (void)registerKVC{
    self.viewModel = [[MVVMViewModel alloc] init];
    [self.viewModel addObserver:self forKeyPath:kVCPath options:0 context:nil];
}
- (void)initData{
    [self.viewModel loadData];
}

- (void)initUI{
    [self.view addSubview:self.tableView];
}
#pragma mark - 事件响应方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:kVCPath]) {
        [self.tableView reloadData];
    }
}
#pragma mark - UITableViewDataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.viewModel tableViewNumberOfSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Mcell];
    cell.imageView.image = [self.viewModel tableViewCellImageWithIndexPath:indexPath];
    return cell;
}
#pragma mark - set方法

#pragma mark - get方法
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        CGRect rect = _tableView.frame;
        rect.origin.y += 20;
        _tableView.frame = rect;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:Mcell];
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
